<?php
/**
 * app 大话水浒
 * @author will
 */
class app_Overlord_Query {
	/**
	 * Collocate添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addCollocate($table , $key , $value){
		$time = time(); //获取时间
		
		$Db = loadDB('app_collocate');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 901;
	}
	/**
	 * Collocate查询全部操作
	 */
	public static function allCollocate($table){
		
		$Db = loadDB('app_collocate');
		
		$result = $Db->getall("SELECT * FROM {$table}");
		
		if(empty($result)) return array();
		if (!empty($result)){
			foreach($result as $k => $row) {
				$result[$k]['Value'] = unserialize($row['Value']);
			}
		}
		return $result;
	}
	/**
	 * Collocate查询单个表
	 */
	public static function oneCollocate($table , $key){
		$Db = loadDB('app_collocate');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * Collocate修改表内容
	 * @param unknown_type $key
	 */
	public static function alterCollocate($table , $key , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_collocate');
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 902;
	}
	/**
	 * app_user添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addUser($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'user_' . ($uin % 64 + 1);
		
		$key = 'user_' .$uin;
		$Db = loadDB('app_user');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 903;
	}
	/**
	 * app_user查询单个表
	 */
	public static function oneUser($uin){
		
		//配表
		$table = 'user_' . ($uin % 64 + 1);
		
		$key = 'user_' .$uin;
	
		$Db = loadDB('app_user');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_user查询多个表
	 */
	public static function allUser($uins){
		
		$Db = loadDB('app_user');

		if(empty($uins)) return array();
		$list = array();
		foreach ($uins as $k => $row){
			//配表
			$table = 'user_' . ($row % 64 + 1);
			
			$key = 'user_' .$row;
			
			$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
		
			if(empty($result)) continue;
			$result['Value'] = unserialize($result['Value']);
			if(!empty($result)) $list[] = $result;
			
		}
		
		if(empty($list)) return array();
		return $list;
	}
	/**
	 * app_user修改表内容
	 * @param unknown_type $key
	 */
	public static function alterUser($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_user');
		//配表
		$table = 'user_' . ($uin % 64 + 1);
		
		$key = 'user_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 904;
	}
	/**
	 * app_city添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addCity($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'city_' . ($uin % 64 + 1);
		
		$key = 'city_' .$uin;
		$Db = loadDB('app_city');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 905;
	}
	/**
	 * app_city查询单个表
	 */
	public static function oneCity($uin){
		
		//配表
		$table = 'city_' . ($uin % 64 + 1);
		
		$key = 'city_' .$uin;
	
		$Db = loadDB('app_city');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_city查询多个表
	 */
	public static function allCity($uins){
		$Db = loadDB('app_city');
		if(empty($uins)) return array();
		foreach ($uins as $k => $row){
			//配表
			$table = 'city_' . ($row % 64 + 1);
			
			$key = 'city_' .$row;
			
			$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
		
			if(empty($result)) continue;
			$result['Value'] = unserialize($result['Value']);
			if(!empty($result)) $list[] = $result;
		}
		if(empty($list)) return array();
		return $list;
	}
	/**
	 * app_city修改表内容
	 * @param unknown_type $key
	 */
	public static function alterCity($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_city');
		//配表
		$table = 'city_' . ($uin % 64 + 1);
		
		$key = 'city_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 906;
	}
	/**
	 * app_consume添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addConsume($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'consume_' . ($uin % 64 + 1);
		
		$key = 'consume_' .$uin;
		$Db = loadDB('app_consume');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 907;
	}
	/**
	 * app_consume查询单个表
	 */
	public static function oneConsume($uin){
		
		//配表
		$table = 'consume_' . ($uin % 64 + 1);
		
		$key = 'consume_' .$uin;
	
		$Db = loadDB('app_consume');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_consume修改表内容
	 * @param unknown_type $key
	 */
	public static function alterConsume($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_consume');
		//配表
		$table = 'consume_' . ($uin % 64 + 1);
		
		$key = 'consume_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 908;
	}
	/**
	 * app_exploit_build添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addExploit_Build($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'exploit_build_' . ($uin % 64 + 1);
		
		$key = 'exploit_' .$uin;
		$Db = loadDB('app_exploit_build');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 909;
	}
	/**
	 * app_exploit_build查询单个表
	 */
	public static function oneExploit_Build($uin){
		
		//配表
		$table = 'exploit_build_' . ($uin % 64 + 1);
		
		$key = 'exploit_' .$uin;
	
		$Db = loadDB('app_exploit_build');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_exploit_build修改表内容
	 * @param unknown_type $key
	 */
	public static function alterExploit_Build($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_exploit_build');
		//配表
		$table = 'exploit_build_' . ($uin % 64 + 1);
		
		$key = 'exploit_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 910;
	}
	/**
	 * app_items添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addItems($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'items_' . ($uin % 64 + 1);
		
		$key = 'items_' .$uin;
		$Db = loadDB('app_items');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);

		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 901;
	}
	/**
	 * app_items查询单个表
	 */
	public static function oneItems($uin){
		
		//配表
		$table = 'items_' . ($uin % 64 + 1);
		
		$key = 'items_' .$uin;
	
		$Db = loadDB('app_items');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_items修改表内容
	 * @param unknown_type $key
	 */
	public static function alterItems($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_items');
		//配表
		$table = 'items_' . ($uin % 64 + 1);
		
		$key = 'items_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 911;
	}
	/**
	 * app_soldiers添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addSoldiers($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'soldiers_' . ($uin % 64 + 1);
		
		$key = 'soldiers_' .$uin;
		$Db = loadDB('app_soldiers');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 912;
	}
	/**
	 * app_soldiers查询单个表
	 */
	public static function oneSoldiers($uin){
		
		//配表
		$table = 'soldiers_' . ($uin % 64 + 1);
		
		$key = 'soldiers_' .$uin;
	
		$Db = loadDB('app_soldiers');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_soldiers修改表内容
	 * @param unknown_type $key
	 */
	public static function alterSoldiers($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_soldiers');
		//配表
		$table = 'soldiers_' . ($uin % 64 + 1);
		
		$key = 'soldiers_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 913;
	}
	/**
	 * app_state添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addState($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'state_' . ($uin % 64 + 1);
		
		$key = 'state_' .$uin;
		$Db = loadDB('app_state');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 914;
	}
	/**
	 * app_state查询单个表
	 */
	public static function oneState($uin){
		
		//配表
		$table = 'state_' . ($uin % 64 + 1);
		
		$key = 'state_' .$uin;
	
		$Db = loadDB('app_state');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_state修改表内容
	 * @param unknown_type $key
	 */
	public static function alterState($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_state');
		//配表
		$table = 'state_' . ($uin % 64 + 1);
		
		$key = 'state_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 915;
	}
	/**
	 * app_task添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addTask($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'task_' . ($uin % 64 + 1);
		
		$key = 'task_' .$uin;
		$Db = loadDB('app_task');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 916;
	}
	/**
	 * app_task查询单个表
	 */
	public static function oneTask($uin){
		
		//配表
		$table = 'task_' . ($uin % 64 + 1);
		
		$key = 'task_' .$uin;
	
		$Db = loadDB('app_task');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_task修改表内容
	 * @param unknown_type $key
	 */
	public static function alterTask($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_task');
		//配表
		$table = 'task_' . ($uin % 64 + 1);
		
		$key = 'task_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 917;
	}
	/**
	 * app_uin_generals添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function addUin_Generals($uin , $value){
		$time = time(); //获取时间
		//配表
		$table = 'uin_generals_' . ($uin % 64 + 1);
		
		$key = 'generals_' .$uin;
		$Db = loadDB('app_uin_generals');
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 918;
	}
	/**
	 * app_uin_generals查询单个表
	 */
	public static function oneUin_Generals($uin){
		
		//配表
		$table = 'uin_generals_' . ($uin % 64 + 1);
		
		$key = 'generals_' .$uin;
	
		$Db = loadDB('app_uin_generals');
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	/**
	 * app_user查询多个表
	 */
	public static function allUin_Generals($uins){
		
		$Db = loadDB('app_uin_generals');
		if(empty($uins)) return array();
		
		foreach ($uins as $k => $row){
			//配表
			$table = 'uin_generals_' . ($row % 64 + 1);
			
			$key = 'generals_' .$row;
			
			$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");

			if(empty($result)) continue;
			$result['Value'] = unserialize($result['Value']);
			if(!empty($result)) $list[] = $result;
			
		}
		if(empty($list)) return array();
		return $list;
	}
	/**
	 * app_uin_generals修改表内容
	 * @param unknown_type $key
	 */
	public static function alterUin_Generals($uin , $value){
		$time = time(); //获取时间
		$Db=loadDB('app_uin_generals');
		//配表
		$table = 'uin_generals_' . ($uin % 64 + 1);
		
		$key = 'generals_' .$uin;
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 919;
	}
	/**
	 * 批量查询
	 * Enter description here ...
	 * @param unknown_type $table
	 * @param unknown_type $key
	 */
	public static function getInOverlord($table , $keys){
		
		if(empty($keys)) return array();
		
		$key_arr = array();
		foreach($keys as $key) {
			$key_arr[] = "'{$key}'";
		}
	
		$key_str = implode($key_arr , ',');
		$Db = loadDB('app_collocate');
		$result = $Db->getAll("SELECT * FROM {$table} WHERE `Key` IN ($key_str)");
		if(empty($result)) return array();
		foreach($result as $k => $row) {
			$result[$k]['Value'] = unserialize($row['Value']);
		}
		return $result;
	}
}
# end of script